<template>
  <div class="list" ref="wrapper">
    <div>
      <div class="area">
        <div class="title">当前城市</div>
        <div class="button-list">
          <div class="button-wrapper">
            <div class="button">{{ this.$store.state.city }}</div>
          </div>
        </div>
      </div>
      <div class="area">
        <div class="title">热门城市</div>
        <div class="button-list">
          <div @click="handleCityClick(item.name)" class="button" v-for="item of hot" :key="item.id">{{ item.name }}</div>
        </div>
      </div>
      <div ref="areaCity">
        <div class="area cityList" v-for="(item, key) of cities" :ref="key">
          <div class="title">{{ key }}</div>
          <div class="item-list" v-for="city of item" :key="city.id">
            <div @click="handleCityClick(city.name)" class="item border-bottom">{{ city.name }}</div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script type="text/ecmascript-6">
import Bscroll from 'better-scroll'
export default{
    name:'CityList',
    props:{
      cities:{
        type:Object
      },
      hot:{
        type:Array
        },
        letter:{
          type:String
        }
    },
    data(){
        return{
            scrollY:0,
            listHeight:[]
        }
    },
    mounted(){
        this.scroll = new Bscroll(this.$refs.wrapper,{
          click:true,
          probeType:3
        })
        this.scroll.on('scroll',(pos) => {
            this.scrollY = Math.abs(Math.round(pos.y))
        })
        this.catculateHeight()
    },
    watch:{
      letter(){
         if(this.letter){
           const element = this.$refs[this.letter][0]
           this.scroll.scrollToElement(element,300)
         }
      },
      scrollY(){
        this.currentIndex()
      }
    },
    methods:{
      catculateHeight(){
        let height = 0
        let cityList = this.$refs.areaCity.getElementsByClassName('cityList')
        this.listHeight.push(height)
        for(let i=0 ; i<cityList.length; i++){
          let item = cityList[i]
          height += item.clientHeight
          this.listHeight.push(height)
        }
      },
      currentIndex(){
          for(let i=0;i<this.listHeight.length;i++){
              let height1 = this.listHeight[i]
              let height2=this.listHeight[i+1]
              if(this.scrollY>=height1 && this.scrollY<height2){
                 this.$emit('current',i)
              }
          }
      },
      handleCityClick(city){
         this.$store.dispatch('changeCity',city)
         this.$router.push('/')
      }
    }
}
</script>

<style lang="stylus" scoped>
@import '~@/assets/styles/varibles.styl'

.border-topbottom
  &:before
    border-color #ccc
  &:after
    border-color #ccc
.border-bottom
  &::before
    border-color #ccc
.list
  overflow hidden
  position absolute
  top 79px
  left 0
  right 0
  bottom 0
  .title
    line-height 27px
    background #eee
    padding-left 10px
    color #666
    font-size 13px
  .button-list
    overflow hidden
    padding 5px 12px 5px 5px
    .button
      float left
      width 29%
      height 18px
      line-height 18px
      margin 5px
      text-align center
      border 1px solid #ccc
  .item-list
    .item
      line-height 38px
      padding-left 10px
</style>
